package tokenserver

import (
	"time"
	"fmt"
	"encoding/json"
	"git.oschina.net/YPTArch/basic/libs/remote"
	"git.oschina.net/YPTArch/usercenter/libs/conv"
	"errors"
	"git.oschina.net/YPTArch/usercenter/libs/data"
	"git.oschina.net/YPTArch/usercenter/config"
)
const (
	address     = "http://192.168.1.183:8081"
)
type (

	RemoteSaveTokenRequest struct {
		// token
		Access           string
		// token创建时间
		AccessCreateAt   int64
		// token过期时间
		AccessExpiresIn  int64
		// 用户信息
		User interface{}
	}


	//远程生成Token 请求
	RemoteGenerateTokenRequest struct {
		// 用户信息
		User interface{}
		// 是否生成刷新Token
		IsGenRefresh bool

	}
	RemoteGenerateTokenByRefreshTokenRequest struct {
		// 刷新Token
		RefreshToken string
	}

	//远程生成Token 返回
	RemoteGenerateTokenResponse struct {
		// token
		Access           string
		// token创建时间
		AccessCreateAt   time.Time
		// token过期时间
		AccessExpiresIn  time.Duration
		// refresh
		Refresh          string
		// refresh token 创建时间
		RefreshCreateAt  time.Time
		// refresh token 过期时间
		RefreshExpiresIn time.Duration
	}
)
// 远程生成token
func RemoteSaveToken(rstr *RemoteSaveTokenRequest) (err error) {
	reqBody,_:=json.Marshal(rstr)
	body, contentType, it, err := remote.HttpPost("http://"+config.GetApiGatewayAddress()+"/usercenter/login",string(reqBody),"application/json")
	fmt.Println(body, contentType, it, err)
	if err != nil {
		fmt.Println(err)
		return errors.New("远程调用失败")
	}
	temp := &data.Result{}
	err = json.Unmarshal([]byte(body), temp)
	fmt.Println("temp=>",temp)

	if err != nil {
		fmt.Println("err=>",err.Error())
		return errors.New("解析失败")
	}
	//当返回数据为空时
	if temp.Code!=200 || temp.Data==nil {
		return errors.New("远程调用失败")
	}
	return nil
}
// 远程生成token
func RemoteGenerateToken(rgtReq *RemoteGenerateTokenRequest) (rgtRes *RemoteGenerateTokenResponse,err error) {
	reqBody,_:=json.Marshal(rgtReq)
	body, contentType, it, err := remote.HttpPost("http://"+config.GetApiGatewayAddress()+"/login",string(reqBody),"application/json")
	fmt.Println(body, contentType, it, err)
	if err != nil {
		fmt.Println(err)
		return nil,errors.New("远程调用失败")
	}
	temp := &data.Result{}
	err = json.Unmarshal([]byte(body), temp)
	fmt.Println(temp)
	if err != nil {
		fmt.Println(err)
		return nil,errors.New("解析失败")
	}
	//当返回数据为空时
	if temp.Code!=200 || temp.Data==nil {
		return nil,errors.New("远程调用失败")
	}
	fmt.Println(temp.Data)
	m:=temp.Data

	rgtRes=new (RemoteGenerateTokenResponse)

	conv.Map2Obj(m,rgtRes,nil)
	fmt.Println(rgtRes)
	return rgtRes,nil
}
// 远程生成token根据刷新Token生成
func RemoteGenerateTokenByRefreshToken(rgtrtReq *RemoteGenerateTokenByRefreshTokenRequest) (rgtRes *RemoteGenerateTokenResponse,err error) {

	reqBody,_:=json.Marshal(rgtrtReq)
	body, contentType, it, err := remote.HttpPost("http://"+config.GetApiGatewayAddress()+"/refresh",string(reqBody),"application/json")
	fmt.Println(body, contentType, it, err)
	if err != nil {
		fmt.Println(err)
		return nil,errors.New("远程调用失败")
	}
	temp := &data.Result{}
	err = json.Unmarshal([]byte(body), temp)
	fmt.Println(temp)
	if err != nil {
		fmt.Println(err)
		return nil,errors.New("解析失败")
	}
	//当返回数据为空时
	if temp.Code!=200 || temp.Data=="" {
		return nil,errors.New("远程调用失败")
	}
	fmt.Println(temp.Data)
	m:=temp.Data

	rgtRes=new (RemoteGenerateTokenResponse)
	conv.Map2Obj(m,rgtRes,nil)
	return rgtRes,nil
}
